<?php

/**
* @Author:  	Daniel Dinh
* @Email:   	webdevelopmentvn@gmail.com
* @Link: 		http://joomsoft.net
* @Version: 	1.0
* @copyright:	Joomsoft.net
* @license:		GNU GPL v3
*/

// no direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.controller');

/**
 * Component Controller
 *
 * @package		Joomla.Administrator
 * @subpackage	com_contact
 */
class importimgController extends JController
{
	/**
	 * @var		string	The default view.
	 * @since	1.6
	 */
	protected $default_view = 'importimg';

	/**
	 * Method to display a view.
	 *
	 * @param	boolean			If true, the view output will be cached
	 * @param	array			An array of safe url parameters and their variable types, for valid values see {@link JFilterInput::clean()}.
	 *
	 * @return	JController		This object to support chaining.
	 * @since	1.5
	 */
	public function display($cachable = false, $urlparams = false)
	{
		parent::display();
		importimgHelper::addSubmenu(JRequest::getCmd('view', 'importimg'));
		return $this;
	}
	
	
	public function importing()
	{
		$params = JComponentHelper::getParams('com_importimg');
		$path = JPATH_ROOT.DS.$params->get('image_folder','images/properties/');
		$api  = $params->get('api_url','http://50.28.60.118:8983/solr/browse?&q=&wt=xml&fl=image');
		$f_step = 1;//$params->get('step',1);
		$f_start = $params->get('start_row',1);
		$thumb_width = $params->get('thumb_width');
		$thumb_height  = $params->get('thumb_height');
			
		
		$row = $_GET['start'];
		if(!$row)
			$row = 0;
			
		$url = $api.'&start='.$row.'&rows='.$f_step;
		$xmlstring =  importimgHelper::post_curl($url);
		jimport('joomla.utilities.simplexml');
		$xml = new JSimpleXML();
		$xml->loadString($xmlstring,'products');	
		$docs =  $xml->document->_children[1]->_children[0]->_children[0]->_children;
		

		if($docs)
		{	$imgs = array();
			foreach($docs as $doc)
				if($doc->_data)
					$imgs[] = $doc->_data;
		}

		if(!is_dir($path))
		{
			mkdir($path,0755);
		}
		if($imgs)
		{
			$img_index = $_GET['img'] ? $_GET['img'] : 0;
			$img = $imgs[$img_index];
			if($img)
			{
				$midpaths= importimgHelper::getMidPath($img,'');
				$filename = basename($img);
				$temp_path = $path;
				
				if($midpaths)
				{
					for($k=0;$k<count($midpaths);$k++)
					{
						
						if(!is_dir($temp_path.$midpaths[$k].DS))
						{
							@mkdir($temp_path.$midpaths[$k].DS ,0755);
						}
						$temp_path = $temp_path.$midpaths[$k].DS;
					}
					//save image
					echo $temp_path.$filename."<br>";
					if(!is_file($temp_path.$filename))
					{
						$content = importimgHelper::get_image_curl($img);
						if($content)
						{
							$fp = fopen($temp_path.$filename,"x");
							fwrite($fp,$content);
							fclose($fp);
							unset($fp);
							file_put_contents($temp_path.$filename,$content);
							@mkdir($temp_path.'thumb');
							copy($temp_path.$filename,$temp_path.'thumb'.DS.$filename);
							//resize
							$resizer = new img_opt();
							$resizer->max_width($thumb_width);
							$resizer->max_height($thumb_height);
							$resizer->image_path($temp_path.'thumb'.DS.$filename);
							$resizer->image_resize();
						}//endif
						
					}//endif;
					
					
				}//endif;
				$img_index ++;
			}//endfor
			else
			{
				$row++;		
				$img_index=0;
			}
		}//endif;
		

		$newurl=JURI::root().'administrator/index.php?option=com_importimg&task=importing&start='.$row.'&img='.$img_index;
		echo '<meta http-equiv="refresh" content="2;url='.$newurl.'"> ';
		
		
	}//end function
	
	
	
	 
}
